import javax.lang.model.type.ReferenceType;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 马拉圈
 * Date: 2023-03-25
 * Time: 16:25
 */
class MyTask implements Comparable{
    public Runnable runnable;
    public long time;
    //绝对时间戳~
    //方便判断~
    //这个不是定时时间

    public MyTask(Runnable runnable, long delay) {
        this.runnable = runnable;
        this.time = delay + System.currentTimeMillis();
    }

    @Override
    public int compareTo(Object o) {
        return (int)(this.time - ((MyTask)o).time);
    }
}
public class MyTimer {
    private PriorityBlockingQueue<MyTask> tasks = new PriorityBlockingQueue<>(5, (o1, o2) -> {
        return (int)(o1.time - o2.time);
    });



    public void schedule(Runnable runnable, long delay) {
        MyTask myTask = new MyTask(runnable, delay);
        tasks.put(myTask);
        synchronized (locker) {
            locker.notify();
            //不唤醒，让他继续等，那个任务必然会启动
            //但是更早时间的任务，就会被延迟，这是不应该出现的状况~
        }
    }

    private Object locker = new Object();

    public MyTimer() {
        Thread t = new Thread(() -> {
            while(true) {
                    try {
                        MyTask myTask = tasks.take();
                        long nowTime = System.currentTimeMillis();
                        if(myTask.time <= nowTime) {
                            myTask.runnable.run();
                        }else {
                            tasks.put(myTask);
                            synchronized (locker) {
                                locker.wait(myTask.time - nowTime);
                            }
                            //安排一个更早的任务，就可以提前唤醒~
                            //当然，可能下一次还是这个任务，但是判断次数大大降低~
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
        });
        t.start();
    }
    public static void main(String[] args) {
        MyTimer timer = new MyTimer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("星期一好耶 ^ v ^");
            }
        },1000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("星期二好耶 ^ v ^");
            }
        },2000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("星期三好耶 ^ v ^");
            }
        },3000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("星期四好耶 ^ v ^");
            }
        },4000);
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("星期五好耶 ^ v ^");
            }
        },5000);
        System.out.println("今天不好耶 T . T");
    }
}
